package com.gljx.web.service.impl;

import com.gljx.util.Axamine;
import com.gljx.util.GetFid;
import com.gljx.util.ServerResponse;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.*;
import com.gljx.web.service.InwareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * Created by rhc on 2018/5/22 0022.
 * 入库管理的功能实现
 */
@Transactional
@Service
public class InwareHouseServiceImpl extends CommonDao implements InwareHouseService
{

    @Autowired
    private InwareHouseMapper inwareHouseMapper;
    @Autowired
    private InwareHouseEntryMapper inwareHouseEntryMapper;
    @Autowired
    private GoodstableMapper goodstableMapper;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.InwareHouseMapper.pagemap", paramMap, null);
        return  paramMap;
    }

    /**
     * 取记录总数      dmz 20160921
     * @param psql   SQL语句
     * @return
     */
    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.InwareHouseMapper.pagecount", paramMap, null);
        return paramMap;
    }

    /**
     * 添加提交
     * @param inwareHouse
     * @param rkGoodsname
     * @param rkGoodsid
     * @param rkGoodstype
     * @param rkGgid
     * @param rkGoodsprice
     * @param rkInwarehouseprice
     * @param rkInwarehousequantity
     * @param rkInwarehousetotalprices
     * @param rkInwarehouseuse
     * @param fesq
     * @param user
     * @param fnumber
     * @param fcreatetime
     */
    @Override
    public void addInwareHouse(InwareHouse inwareHouse, String[] rkGoodsname, String[] rkGoodsid, String[] rkGoodstype, String[] rkGgid,
                               String[] rkGoodsprice, String[] rkInwarehouseprice, String[] rkInwarehousequantity, String[] rkInwarehousetotalprices, String[] rkInwarehouseuse,String[]fesq, User user,
                               String fnumber,String fcreatetime) {
        //随机建成入库表的fid
        String fid = GetFid.UUIDIntoFid();
        inwareHouse.setFid(fid);
        //获取创建者id
        inwareHouse.setFcreatorid(user.getFid());
        //获取控制单元
        inwareHouse.setFcontrolunitid(user.getFcompanyid());
        //生成创建者id
        inwareHouse.setFcreatorid(user.getFid());
        //生成创建人名称
        inwareHouse.setCreatename(user.getName());
        //生成公司名称
        inwareHouse.setCompanyname(user.getCompanyName());
        //生成创建状态
        inwareHouse.setCfbillstate(3L);
        //生成公司id
        inwareHouse.setCfficompanyid(user.getFcompanyid());
        //生成创建日期
        inwareHouse.setFcreatetime(fcreatetime);
        //生成订单号码
        inwareHouse.setFnumber(fnumber);
        //添加到InwareHouse表中
        inwareHouseMapper.insertSelective(inwareHouse);
        //建立一个新 InwareHouseEntry集合
        List<InwareHouseEntry> inwareHouseEntries = new ArrayList<>();
        for (int i = 0; i < rkGoodsname.length; i++) {
            //创建一个新的InwareHouseEntry类
            InwareHouseEntry inwareHouseEntry = new InwareHouseEntry();
            String fidsmall=GetFid.UUIDIntoFid();
            //入库表id

            if(fidsmall!=null&&!"".equals(fidsmall)){
                inwareHouseEntry.setFid(fidsmall);
            }else{
                inwareHouseEntry.setFid("无"+GetFid.UUIDIntoFid());
            }
            //赋值入库物品名
            if(rkGoodsname[i]!=null&&!"".equals(rkGoodsname[i])){
                inwareHouseEntry.setRkGoodsname(rkGoodsname[i]);
            }else{
                inwareHouseEntry.setRkGoodsname("无");
            }
            //赋值入库物品id
            if(rkGoodsid[i]!=null&&!"".equals(rkGoodsid[i])){
                inwareHouseEntry.setRkGoodsid(rkGoodsid[i]);
            }else{
                inwareHouseEntry.setRkGoodsid("无");
            }
            //赋值入库物品类型
            if(rkGoodstype[i]!=null&&!"".equals(rkGoodstype[i])){
                inwareHouseEntry.setRkGoodstype(rkGoodstype[i]);
            }else{
                inwareHouseEntry.setRkGoodstype("无");
            }
            //赋值物品规格
            if(rkGgid[i]!=null&&!"".equals(rkGgid[i])){
                inwareHouseEntry.setRkGgid(rkGgid[i]);
            }else{
                inwareHouseEntry.setRkGgid("无");
            }
//                物品单价
            if(rkGoodsprice[i]!=null&&!"".equals(rkGoodsprice[i])){
                inwareHouseEntry.setRkGoodsprice(BigDecimal.valueOf(Double.parseDouble(rkGoodsprice[i])));
            }else{
                inwareHouseEntry.setRkGoodsprice(BigDecimal.valueOf(Double.parseDouble("0.00")));
            }
//                 入库物品单价
            if(rkInwarehouseprice[i]!=null&&!"".equals(rkInwarehouseprice[i])){
                inwareHouseEntry.setRkInwarehouseprice(BigDecimal.valueOf(Double.parseDouble(rkInwarehouseprice[i])));
            }else{
                inwareHouseEntry.setRkInwarehouseprice(BigDecimal.valueOf(Double.parseDouble("0.00")));
            }
//                 入库数量
            if(rkInwarehousequantity[i]!=null&&!"".equals(rkInwarehousequantity[i])){
                inwareHouseEntry.setRkInwarehousequantity(Long.parseLong(rkInwarehousequantity[i]));
            }else{
                inwareHouseEntry.setRkInwarehousequantity(0l);
            }
//                 入库总价
            if(rkInwarehousetotalprices[i]!=null&&!"".equals(rkInwarehousetotalprices[i])){
                inwareHouseEntry.setRkInwarehousetotalprices(BigDecimal.valueOf(Double.parseDouble(rkInwarehousetotalprices[i])));
            }else{
                inwareHouseEntry.setRkInwarehousetotalprices(BigDecimal.valueOf(Double.parseDouble("0")));
            }
//                 继续赋值
            inwareHouseEntry.setFparentid(fid);

            //入库用途
            if(rkInwarehouseuse[i]!=null&&!"".equals(rkInwarehouseuse[i])){
                inwareHouseEntry.setRkInwarehouseuse(rkInwarehouseuse[i]);
            }else{
                inwareHouseEntry.setRkInwarehouseuse("暂无");
            }
            if(fesq[i]!=null&&!"".equals(fesq[i])){
                inwareHouseEntry.setFseq(Long.parseLong(fesq[i]));
            }else{
                inwareHouseEntry.setFseq(Long.parseLong("0l"));
            }
//                 加入集合
            inwareHouseEntries.add(inwareHouseEntry);
        }
        //存入表中，批量增加
        inwareHouseEntryMapper.addInwareHouseEntry(inwareHouseEntries);
    }
    //批量修改
    @Override
    public void editInwareHouse(InwareHouse inwareHouse, String[] rkGoodsname, String[] rkGoodsid, String[] rkGoodstype, String[] rkGgid, String[] rkGoodsprice, String[] rkInwarehouseprice, String[] rkInwarehousequantity, String[] rkInwarehousetotalprices, String[] rkInwarehouseuse, User user, String time,String []fseq) {

        SimpleDateFormat simp=new SimpleDateFormat("yyyy-MM-dd");
        //输出日期
        time=simp.format(new Date());

        inwareHouse.setAuditname(user.getName());
        //生成修改日期
        inwareHouse.setFlastupdatetime(time);
        //添加到InwareHouse表中
        inwareHouseMapper.updateByPrimaryKeySelective(inwareHouse);
        //建立一个新 InwareHouseEntry集合
//        List<InwareHouseEntry> inwareHouseEntries =inwareHouseEntryMapper.selecInwareHousetByfparentid(inwareHouse.getFid());
        //删除所有子类信息
        inwareHouseEntryMapper.deleteByfparemtId(inwareHouse.getFid());
       //建立一个新的集合
        List<InwareHouseEntry> inwareHouseEntries1=new ArrayList<>();
        for(int i=0;i<rkGoodsname.length;i++){
            //创建一个新的InwareHouseEntry类
            InwareHouseEntry inwareHouseEntry = new InwareHouseEntry();
            String fidsmall=GetFid.UUIDIntoFid();
            //入库表id

            if(fidsmall!=null&&!"".equals(fidsmall)){
                inwareHouseEntry.setFid(fidsmall);
            }else{
                inwareHouseEntry.setFid("无"+GetFid.UUIDIntoFid());
            }
            //赋值入库物品名
            if(rkGoodsname[i]!=null&&!"".equals(rkGoodsname[i])){
                inwareHouseEntry.setRkGoodsname(rkGoodsname[i]);
            }else{
                inwareHouseEntry.setRkGoodsname("无");
            }
            //赋值入库物品id
            if(rkGoodsid[i]!=null&&!"".equals(rkGoodsid[i])){
                inwareHouseEntry.setRkGoodsid(rkGoodsid[i]);
            }else{
                inwareHouseEntry.setRkGoodsid("无");
            }
            //赋值入库物品类型
            if(rkGoodstype[i]!=null&&!"".equals(rkGoodstype[i])){
                inwareHouseEntry.setRkGoodstype(rkGoodstype[i]);
            }else{
                inwareHouseEntry.setRkGoodstype("无");
            }
            //赋值物品规格
            if(rkGgid[i]!=null&&!"".equals(rkGgid[i])){
                inwareHouseEntry.setRkGgid(rkGgid[i]);
            }else{
                inwareHouseEntry.setRkGgid("无");
            }
//                物品单价
            if(rkGoodsprice[i]!=null&&!"".equals(rkGoodsprice[i])){
                inwareHouseEntry.setRkGoodsprice(BigDecimal.valueOf(Double.parseDouble(rkGoodsprice[i])));
            }else{
                inwareHouseEntry.setRkGoodsprice(BigDecimal.valueOf(Double.parseDouble("0.00")));
            }
//                 入库物品单价
            if(rkInwarehouseprice[i]!=null&&!"".equals(rkInwarehouseprice[i])){
                inwareHouseEntry.setRkInwarehouseprice(BigDecimal.valueOf(Double.parseDouble(rkInwarehouseprice[i])));
            }else{
                inwareHouseEntry.setRkInwarehouseprice(BigDecimal.valueOf(Double.parseDouble("0.00")));
            }
//                 入库数量
            if(rkInwarehousequantity[i]!=null&&!"".equals(rkInwarehousequantity[i])){
                inwareHouseEntry.setRkInwarehousequantity(Long.parseLong(rkInwarehousequantity[i]));
            }else{
                inwareHouseEntry.setRkInwarehousequantity(0l);
            }
//                 入库总价
            if(rkInwarehousetotalprices[i]!=null&&!"".equals(rkInwarehousetotalprices[i])){
                inwareHouseEntry.setRkInwarehousetotalprices(BigDecimal.valueOf(Double.parseDouble(rkInwarehousetotalprices[i])));
            }else{
                inwareHouseEntry.setRkInwarehousetotalprices(BigDecimal.valueOf(Double.parseDouble("0")));
            }
//                 继续赋值
            inwareHouseEntry.setFparentid(inwareHouse.getFid());

            //入库用途
            if(rkInwarehouseuse[i]!=null&&!"".equals(rkInwarehouseuse[i])){
                inwareHouseEntry.setRkInwarehouseuse(rkInwarehouseuse[i]);
            }else{
                inwareHouseEntry.setRkInwarehouseuse("暂无");
            }
            if(fseq[i]!=null&&!"".equals(fseq[i])){
                inwareHouseEntry.setFseq(Long.parseLong(fseq[i]));
            }else{
                inwareHouseEntry.setFseq(Long.parseLong("0l"));
            }
//                 加入集合
            inwareHouseEntries1.add(inwareHouseEntry);
        }
        //存入表中，批量增加
        inwareHouseEntryMapper.addInwareHouseEntry(inwareHouseEntries1);
}
    //批量删除
    @Override
    public void deleteInwareHouseByFid(String fid, String[] deleteId, String[] fnumber, String fnumber2) {
        if (fid == null|| "".equals(fid)) {
            //先删除子类信息
            inwareHouseEntryMapper.deleteInwareHouseEntry(deleteId);
            //再删除父类信息
            inwareHouseMapper.deleteByArray(deleteId);
        } else {
            //删除子类信息
            inwareHouseEntryMapper.deleteByfparemtId(fid);
            //在删除父类元素
            inwareHouseMapper.deleteByPrimaryKey(fid);
        }

    }
    // 按照fid查询父表
    @Override
    public InwareHouse selectByfid(String fid) {

        return inwareHouseMapper.selectByPrimaryKey(fid);
    }

    @Override
    public List<InwareHouse> selectInwareHouseByCreaterId(String fcreatorid) {
        //通过创建者id进行查询获取list InwareHouse
        List<InwareHouse> inwareHouses = inwareHouseMapper.selectInwareHouseByfcreatorid(fcreatorid);

        return inwareHouses;


    }
    //通过父表id得到父表下面的所有的子表
    @Override
    public List<InwareHouseEntry> queryInwareHouseEntry(String fparentid) {
        //通过父类id进行查询获取list InwareHouseEntry
        List<InwareHouseEntry> inwareHouseEntries = inwareHouseEntryMapper.selecInwareHousetByfparentid(fparentid);

        return inwareHouseEntries;
    }

    /**
     * 通过父表的fid查询
     * @param fid
     * @return
     */
    @Override
    public InwareHouseEntry selectInwareHouseById(String fid) {


        return inwareHouseEntryMapper.selectByPrimaryKey(fid);
    }

    /**
     * 三者查询
     * @param fparentid
     * @param goodsName
     * @param goodsType
     * @return
     */
    @Override
    public List<InwareHouseEntry> selectInwareHouseEntry(String fparentid, String goodsName, String goodsType) {
        //通过父类id进行查询获取list InwareHouseEntry
        List<InwareHouseEntry>     inwareHouseEntries = inwareHouseEntryMapper.selecInwareHousetBygoodsStyle(fparentid,goodsName,goodsType);
        return inwareHouseEntries;
    }

    @Override
    public int updateInwareHouse(InwareHouse inwareHouse) {
        return inwareHouseMapper.updateByPrimaryKeySelective(inwareHouse);
    }
    //审核
    @Override
    public int auditInwaHouse(String fid, String type, String fhandlerId, List<InwareHouseEntry> inwareHouseEntries, String message, String state, String fnumber,User user) {
        // TODO Auto-generated method stub
        InwareHouse inwareHouse = new InwareHouse();
        //fid
        inwareHouse.setFid(fid);

        //修改人
        inwareHouse.setFlastupdateuserid(fhandlerId);
        Axamine axamine = new Axamine();
        if ("1".equals(type)) {
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            inwareHouse.setCfbillstate(5L);

            if (serverResponse.getStatus() == 1) {
                inwareHouse.setAuditname(user.getName());
                //审核人id
                inwareHouse.setFauditorid(fhandlerId);
                return inwareHouseMapper.updateByPrimaryKeySelective(inwareHouse);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    inwareHouse.setFdescription(message);
                    inwareHouse.setCfbillstate(3L);
                    //审核人id
                    inwareHouse.setFauditorid("");
                    inwareHouseMapper.updateByPrimaryKeySelective(inwareHouse);
                    return 2;
                }
            } else {
                //错误
                return 0;
            }
            return 0;
        }else{
            axamine.againstAuditProcess(fnumber, message);
            inwareHouse.setCfbillstate(3L);
            //审核人id
            inwareHouse.setFauditorid("");
            return inwareHouseMapper.updateByPrimaryKeySelective(inwareHouse);
        }
    }

    /**
     *导出
     * @param fcompanyid
     * @param rkGoodsname
     * @param rkGoodstype
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public List<InwareHouseEntry> selectInwareHouseByInfo(String fcompanyid, String rkGoodsname, String rkGoodstype, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("fcompanyid", fcompanyid);
        map.put("rkGoodsname", rkGoodsname);
        map.put("rkGoodstype", rkGoodstype);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        return inwareHouseEntryMapper.selectInwareHouseByInfo(map);
    }

}
